package com.woniuxy.book87.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.book87.entity.Book;
import com.woniuxy.book87.entity.BookType;
import com.woniuxy.book87.result.Result;
import com.woniuxy.book87.service.BookService;
import com.woniuxy.book87.vo.BookVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.UUID;

@RestController
@RequestMapping("book")
public class BookController {
    @Autowired
    BookService bookService ;

    @GetMapping("/list")
    public Result<PageInfo<BookVo>> list(@RequestParam(value = "now",defaultValue = "1")Integer  now,
                                         @RequestParam(value = "size",defaultValue = "3")Integer  size,
                                         @RequestParam(value = "typeId",defaultValue = "-1")Integer  typeId,
                                         String name){
        try {
            PageInfo<BookVo> pageInfo = bookService.findBookByPage(now,size,typeId,name);
            return Result.ok("查询图书成功",pageInfo);
        }catch (Exception e) {
            e.printStackTrace();
            return Result.fail("查询图书失败!");
        }
    }

    @PostMapping("/add")
    public Result<String> add(@RequestBody Book book) {
        try {
            bookService.add(book);
            return Result.ok("添加图书成功!");
        }catch (Exception e) {
            e.printStackTrace();
            return Result.fail("添加图书失败!");
        }
    }

    @PostMapping("/upload")
    public Result<String> upload(@RequestParam("uploadImg") MultipartFile uploadImg){
        try {
            //1. 获取上传文件的名字
            String filename = uploadImg.getOriginalFilename();
            //2.修改上传文件的名字
            String uuid = UUID.randomUUID().toString().replaceAll("-","").toUpperCase();
            String subFix = filename.substring(filename.lastIndexOf("."));
            filename = "WONIU"+uuid + subFix ;

            //3.获取上传路径
            ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = servletRequestAttributes.getRequest();
            String path = request.getServletContext().getRealPath("/asserts/imgs");
            File file = new File(path);
            if (!file.exists()) {
                file.mkdirs();
            }
            //4.创建上传文件的对象
            File f = new File(file,filename);
            //5。上传
            uploadImg.transferTo(f);

            return Result.ok("上传成功",filename);
        }catch (Exception e) {
            e.printStackTrace();
            return Result.fail("上传失败!");
        }
    }


    @GetMapping("/getBookByKey/{id}")
    public Result<Book> add(@PathVariable("id") Long id) {
        try {
            Book book = bookService.findBookByKey(id);
            return Result.ok("查询成功!",book);
        }catch (Exception e) {
            e.printStackTrace();
            return Result.fail("查询失败!");
        }
    }
    @PutMapping("/modify")
    public Result<String> modify(@RequestBody Book book) {
        try {
            bookService.modify(book);
            return Result.ok("修改图书成功!");
        }catch (Exception e) {
            e.printStackTrace();
            return Result.fail("修改图书失败!");
        }
    }

}
